package com.amikomgamedev.labirinhantu.state;

import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.amikomgamedev.labirinhantu.Server_Data;
import com.amikomgamedev.labirinhantu.Utils;

public class State_Menu_Level	extends		State_Game_Play
{
   private final float DURATION	= 0.3f;
   private float click_Start	= 0;
   
//   private boolean click	= false;
    
	protected void create_Menu_Level()
	{
//	    m_Camera_Menu.initCamera(grafik.spr_Menu_Level_Bg);
//	    m_Camera_Menu.centerCamera();
//		isCameraMove = false;
		
		grafik.attachMenuLevelBg(Server_Data.getStage());
		grafik.attachMenuLevelBtn();
		grafik.attachMenuLevelGembok(m_DB);
		grafik.attachMenuLevelTulang(m_DB);
		grafik.attachMenuLevelLingkaran();
		teks.attachMenuLevel();
		
		if (Server_Data.getMap() < 10)
		{
        		if (grafik.lst_Menu_Level_Lingkaran.size() == 2)
        		{
                		grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(0);
                		grafik.lst_Menu_Level_Lingkaran.get(1).setCurrentTileIndex(1);
        		}
        		else if (grafik.lst_Menu_Level_Lingkaran.size() == 1)
        		{
        		    	grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(0);		    
        		}
		}
		else
		{
		    	grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(1);
    			grafik.lst_Menu_Level_Lingkaran.get(1).setCurrentTileIndex(0);
		}
	    m_Camera_Menu.initCamera(grafik.spr_Menu_Level_Bg);
	    m_Camera_Menu.centerCamera();
		
		
		
		
		sound.stopMenuBgm();
		sound.playGameBgm(Server_Data.getStage());
		
//		for (int i = 0; i < 2; i++)
//		{
//			grafik.rec_Menu_Level_Bg[i].setVisible(true);
//		    
//		}
	}
	
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		if (State_Menu_Current == STATE_MENU_LEVEL)
		{			
			m_ClickDetector.onTouchEvent(pSceneTouchEvent);
			m_Scroll.onTouchEvent(pSceneTouchEvent);
		}
		
		return super.onSceneTouchEvent(pScene, pSceneTouchEvent);
	}
	
	@Override
	public void onClick(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY)
	{
		if (State_Menu_Current == STATE_MENU_LEVEL)
		{
//			for (int i = 0; i < grafik.spr_Menu_Level_Btn[1].length; i++)
//			{
//			    if (i < grafik.spr_Menu_Level_Btn[1].length - grafik.lst_Menu_Level_Btn_Gembok.size())
//			    	if(Utils.isOnArea(
//						pSceneX, pSceneY,
//						grafik.spr_Menu_Level_Bg,
//						grafik.rec_Menu_Level_Bg[0],
//						grafik.spr_Menu_Level_Btn[1][i]))
//				{
////				    Utils.TRACE("BTN = " + grafik.spr_Menu_Level_Btn[1].length);
//			    	    
//					Server_Data.setMap(i);
//					switchState(STATE_GAME_PLAY);
//					return;
//				}			
//			}
		}
		super.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
	}
	
	@Override
	    public boolean onAreaTouched(
		    TouchEvent pSceneTouchEvent,
		    ITouchArea pTouchArea,
		    float pTouchAreaLocalX,
		    float pTouchAreaLocalY)
	    {
	    	if (State_Menu_Current == STATE_MENU_LEVEL)
		{
	    	    if (pSceneTouchEvent.isActionDown())
	    	    {
	    		click_Start = pTouchAreaLocalX;
	    		Utils.TRACE("TRUE");
	    	    }
	    	    else if (pSceneTouchEvent.isActionUp())
	    		if (click_Start - pTouchAreaLocalX < Utils.getRatio(20)
	    			&& click_Start - pTouchAreaLocalX > -Utils.getRatio(20))
        	    	    for (int i = 0; i < MAP[Server_Data.getStage()].length; i++)
        	    	    {

        		    	    Utils.TRACE("INI MENU LEVEL");
        	    		if (pTouchArea.equals(grafik.spr_Menu_Level_Btn[i]))
        	    		{
//        	    		    if (m_DB.getTulang(Server_Data.getStage(), i) != 0)
//        		    	    Utils.TRACE("INI MENU LEVEL" + i);
        	    		    if (i < grafik.MAP[Server_Data.getStage()].length - grafik.lst_Menu_Level_Btn_Gembok.size())
        	    			{
                	    			Server_Data.setMap(i);
                	    			switchState(STATE_GAME_PLAY);
        	    			}
        	    		}
        	    	    }
		}
		
	    	return super.onAreaTouched(
			pSceneTouchEvent,
			pTouchArea,
			pTouchAreaLocalX,
			pTouchAreaLocalY);
	    }
	
	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		if (State_Menu_Current == STATE_MENU_LEVEL)
		{
			if (isCameraMove)
			{
				m_Camera_Menu.moveCamera(pSecondsElapsed);
				
				if (m_Camera_Menu.isStopCamera())
					switchState(STATE_MENU_MAIN);
			}
		}
		
		super.onUpdate(pSecondsElapsed);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		if(State_Menu_Current == STATE_MENU_LEVEL)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
			{				
				runOnUpdateThread(new Runnable()
				{
				    
				    public void run()
				    {
					grafik.detachMenuLevelBg();
					grafik.detachMenuLevelBtn();
					grafik.detachMenuLevel();
					grafik.detachMenuLevelLingkaran();
					teks.detachMenuLevel();
				    }
				});

				switchState(STATE_MENU_MAIN);
//				m_Camera_Menu.initCamera(grafik.spr_Menu_Main_Bg);				
//				isCameraMove	= true;
				sound.stopGameBgm(Server_Data.getStage());
			}
		}
			
		return super.onKeyUp(keyCode, event);
	}
	
	@Override
	public void onScrollStarted(
	    ScrollDetector pScollDetector,
	    int pPointerID,
	    float pDistanceX,
	    float pDistanceY)
	{
	    	if(State_Menu_Current == STATE_MENU_LEVEL)
        	{
	    	    
        	}
	    	
	    	super.onScrollStarted(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	@Override
	public void onScroll(
	    ScrollDetector pScollDetector,
	    int pPointerID,
	    float pDistanceX,
	    float pDistanceY)
	{
        	if(State_Menu_Current == STATE_MENU_LEVEL)
            	{
//
//		    Utils.TRACE("X  = " +RES_WIDTH);
//		    Utils.TRACE("CenterX  = " +m_Camera.getCenterX());
        	    
		    for (int i = 0; i < grafik.lst_Menu_Level_Lingkaran.size(); i++)
		    {
        		grafik.rec_Menu_Level_Bg[i].setPosition(
        			grafik.rec_Menu_Level_Bg[i].getX() + pDistanceX, 
        			grafik.rec_Menu_Level_Bg[i].getY());			
		    }
        	}
        	super.onScroll(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	@Override
	public void onScrollFinished(
	    ScrollDetector pScollDetector,
	    int pPointerID,
	    float pDistanceX,
	    float pDistanceY)
	{
	    	if(State_Menu_Current == STATE_MENU_LEVEL)
        	{
	    	    
	    	if (grafik.lst_Menu_Level_Lingkaran.size() == 2)
		    {
        	    	if (grafik.rec_Menu_Level_Bg[0].getX() + grafik.rec_Menu_Level_Bg[0].getWidth() <= m_Camera.getCenterX())
        		{
        		    grafik.rec_Menu_Level_Bg[0].registerEntityModifier(
        			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[0].getX(), - RES_WIDTH));
        		    grafik.rec_Menu_Level_Bg[1].registerEntityModifier(
        			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[1].getX(), 0));
        		    

				grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(1);
				grafik.lst_Menu_Level_Lingkaran.get(1).setCurrentTileIndex(0);
        		}
        	    	else if (grafik.rec_Menu_Level_Bg[1].getX() >= m_Camera.getCenterX())
        		{
        		    grafik.rec_Menu_Level_Bg[0].registerEntityModifier(
        			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[0].getX(), 0));
        		    grafik.rec_Menu_Level_Bg[1].registerEntityModifier(
        			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[1].getX(), RES_WIDTH));
        		    
				grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(0);
				grafik.lst_Menu_Level_Lingkaran.get(1).setCurrentTileIndex(1);
        		}
        	    	else
        	    	{
        	    	    if (grafik.rec_Menu_Level_Bg[0].getX() <= - grafik.rec_Menu_Level_Bg[0].getWidth() / 2)
        	    	    {
        	    		grafik.rec_Menu_Level_Bg[0].registerEntityModifier(
            			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[0].getX(), 0));
        	    		grafik.rec_Menu_Level_Bg[1].registerEntityModifier(
        			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[1].getX(), RES_WIDTH));
        	    		
				grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(0);
				grafik.lst_Menu_Level_Lingkaran.get(1).setCurrentTileIndex(1);
        	    	    }
        	    	    else
        	    	    {
                		grafik.rec_Menu_Level_Bg[0].registerEntityModifier(
                			new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[0].getX(), - RES_WIDTH));
        	    		grafik.rec_Menu_Level_Bg[1].registerEntityModifier(
                			new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[1].getX(), 0));
        	    		
				grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(1);
				grafik.lst_Menu_Level_Lingkaran.get(1).setCurrentTileIndex(0);
        	    	    }
        	    	}
		    }
        	    	else if (grafik.lst_Menu_Level_Lingkaran.size() == 1)
        	    	{
                	    	if (grafik.rec_Menu_Level_Bg[0].getX() + grafik.rec_Menu_Level_Bg[0].getWidth() <= m_Camera.getCenterX())
                		{
                		    grafik.rec_Menu_Level_Bg[0].registerEntityModifier(
                			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[0].getX(), 0));
                		    
        
        				grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(0);
                		}
                	    	else 
                		{
                		    grafik.rec_Menu_Level_Bg[0].registerEntityModifier(
                			    new MoveXModifier(DURATION, grafik.rec_Menu_Level_Bg[0].getX(), 0));
                		    
        				grafik.lst_Menu_Level_Lingkaran.get(0).setCurrentTileIndex(0);
                		}
        	    	}
        	}
	    	
	    	super.onScrollFinished(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_MENU_LEVEL)
		sound.stopGameBgm(Server_Data.getStage());
	    
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    if(State_Menu_Current == STATE_MENU_LEVEL)
		sound.playGameBgm(Server_Data.getStage());

	    super.onResumeGame();
	}
}
